package com.article.controller.system;

import com.article.dto.ReturnError;
import com.article.enums.ReturnInfoEnum;
import com.article.model.ArticleType;
import com.article.service.ArticleTypeService;
import com.article.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by Administrator on 2017/5/3.
 */
@RestController
@RequestMapping("/articleTypeAdmin")
public class ArticleTypeAdminController {
    private final static byte[] lock = new byte[0]; // 特殊的instance变量
    private final static Logger logger = LoggerFactory.getLogger(ArticleTypeAdminController.class);
    @Autowired
    private ArticleTypeService articleTypeService;

    /**
     * 文章类型
     *
     * @param key
     * @param pageIndex
     * @param limit
     * @return
     */
    @RequestMapping(value = "/listArticleType", method = RequestMethod.POST)
    public ReturnError listArticleType(@RequestParam(name = "key", required = false) String key,
                                       @RequestParam(name = "pageIndex", required = true, defaultValue = "0") Integer pageIndex,
                                       @RequestParam(name = "limit", required = true, defaultValue = "10") Integer limit) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<ArticleType> articleTypeList = new ArrayList<ArticleType>();
        try {
            if (StringUtil.isNotEmpty(key)) {
                map.put("typeNameLike", key);
            }
            PageHelper.startPage(pageIndex + 1, limit);
            articleTypeList = articleTypeService.listByPage(map);
            PageInfo<ArticleType> p = new PageInfo<ArticleType>(articleTypeList);
            return new ReturnError(false, null, p.getTotal(), p.getList());
        } catch (Exception e) {
            logger.info(ReturnInfoEnum.ERROR.getType(), e);
            return new ReturnError(true, ReturnInfoEnum.ERROR.getInfo(), 0L, null);
        }
    }

    /**
     * 保存类型
     *
     * @param articleType
     * @return
     */
    @RequestMapping(value = "/saveType", method = RequestMethod.POST)
    public ReturnError saveType(ArticleType articleType) {
        try {
            if (StringUtil.isEmpty(articleType.getTypeName())) {
                return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), "请输入类别名", null);
            }
            if (StringUtil.isEmpty("" + articleType.getSort())) {
                return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), "请输入序号", null);
            }
            articleType.setGmtCreate(Calendar.getInstance().getTime());
            articleType.setGmtModified(articleType.getGmtCreate());
            return articleTypeService.saveType(articleType);
        } catch (Exception e) {
            logger.info(ReturnInfoEnum.ERROR.getInfo(), e);
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }

    /**
     * 修改类型
     *
     * @param id
     * @param typeName
     * @param sort
     * @return
     */
    @RequestMapping(value = "/updateType", method = RequestMethod.POST)
    public ReturnError updateType(@RequestParam(name = "id", required = true) Long id,
                                  @RequestParam(name = "typeName", required = true) String typeName,
                                  @RequestParam(name = "sort", required = true) Integer sort) {
        try {
            if (StringUtil.isEmpty(typeName)) {
                return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), "请输入类别名", null);
            }
            if (StringUtil.isEmpty("" + sort)) {
                return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), "请输入序号", null);
            }
            ArticleType articleType = articleTypeService.getById(id);
            if (articleType == null) {
                return new ReturnError(true, ReturnInfoEnum.NORECORD.getType(), ReturnInfoEnum.NORECORD.getInfo(), null);
            }
            articleType.setTypeName(typeName);
            articleType.setSort(sort);
            articleType.setGmtModified(Calendar.getInstance().getTime());
            return articleTypeService.updateType(articleType);
        } catch (Exception e) {
            logger.info(ReturnInfoEnum.ERROR.getInfo(), e);
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }

    /**
     * 删除文章类型
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/removeType", method = RequestMethod.POST)
    public ReturnError removeType(@RequestParam(name = "ids", required = true) String ids) {
        try {
            return articleTypeService.removeType(ids);
        } catch (Exception e) {
            logger.info(ReturnInfoEnum.ERROR.getInfo(), e);
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }
}
